package com.ykt.controller;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.ykt.converter.MerchantConverter;
import com.ykt.model.Shop;
import com.ykt.model.User;
import com.ykt.model.UserFeedback;
import com.ykt.service.CaptchaService;
import com.ykt.service.ShopService;
import com.ykt.service.UserFeedbackService;
import com.ykt.service.UserService;
import com.ykt.validator.MerchantValidator;
import com.ykt.vo.MerchantVo;
import com.ykt.vo.ResultStatus;
import com.ykt.vo.ResultVo;

import java.util.Date;
import java.util.List;

/**
 * 商户API
 *
 * @author wing
 * @version 1.0.0
 */
public class MerchantController extends BaseController {

    private UserService userService = enhance(UserService.class.getName(), UserService.class);
    private ShopService shopService = enhance(ShopService.class.getName(), ShopService.class);
    private CaptchaService captchaService = enhance(CaptchaService.class.getName(), CaptchaService.class);
    private UserFeedbackService userFeedbackService = enhance(UserFeedbackService.class.getName(), UserFeedbackService.class);


    @Before(MerchantValidator.LoginValidator.class)
    public void login() {
        String phone = getPara("phone");
        String checkCode = getPara("checkCode");

        // 验证短信验证码
        String captcha = captchaService.getCaptcha(phone);
        if (checkCode == null || !checkCode.equals(captcha)) {
            returnJson(ResultVo.failure(null, "验证码无效，请重试！"));
            return;
        }

        // 查询商户
        User user = userService.find(phone);
        if (user == null) {
            returnJson(new ResultVo<>(null, ResultStatus.FAILURE, "该手机号未注册，请注册！"));
            return;
        }

        // 是否为商户
        if(!User.Type.MERCHANT.name().equals(user.getRole()) && !User.Type.ALL.name().equals(user.getRole())){
            returnJson(new ResultVo<>(null, ResultStatus.FAILURE, "该手机号未注册成商户，请注册！"));
            return;
        }

        // 查询商铺
        MerchantVo vo = MerchantConverter.convert(user);
        List<Shop> shops = shopService.findByMerchantId(vo.getId());
        if (shops != null && !shops.isEmpty()) {

            vo.setShopId(shops.get(0).getId());
        }

        this.returnJson(ResultVo.success(vo));
    }

    @Before({MerchantValidator.UpdateValidator.class})
    public void modify() {
        //客户端所传参数
        Integer userId = getParaToInt("merchantId", -1);
        String name = getPara("name");

        User model = userService.find(userId);
        if (StrKit.notBlank(name)) {
            model.set("name", name);
        }

        int id = userService.save(model);
        if (id > 0) {
            returnJson(ResultVo.success(id));
        } else {
            returnJson(ResultVo.failure(null, ResultStatus.UPDATE_FAILURE_MSG));
        }
    }


    @Before({MerchantValidator.UpdateHeadValidator.class})
    public void uploadHead() {
        //客户端所传参数
        Integer userId = getParaToInt("merchantId", -1);
        String head = getPara("headFile");

        User model = userService.find(userId);
        model.set("head", head);

        int id = userService.save(model);
        if (id > 0) {
            returnJson(ResultVo.success());
        } else {
            returnJson(ResultVo.failure(null, ResultStatus.UPDATE_FAILURE_MSG));
        }
    }

    /**
     * 用户反馈
     */
    @Before({MerchantValidator.FeedbackValidator.class})
    public void feedback() {
        Integer merchantId = getParaToInt("merchantId", -1);
        String feedback = getPara("feedback");
        String email = getPara("email");

        UserFeedback model = new UserFeedback();
        model.set(UserFeedback.user_id, merchantId);
        model.set(UserFeedback.feedback, feedback);
        model.set(UserFeedback.email, email);
        model.set(UserFeedback.isread, 0);// 是否阅读：0未读，1已读
        model.set(UserFeedback.created_time, new Date());

        int id = userFeedbackService.save(model);

        this.returnJson(ResultVo.success(id));
    }
}
